package ext.vpde.cadvaulting.migration;

import java.io.*;
import java.util.*;

import ext.vpde.cadvaulting.util.WINDSQL;
import wt.adapter.BasicWebjectDelegate;
import wt.epm.familytable.*;
import wt.fc.*;
import wt.folder.*;
import wt.vc.wip.CheckoutLink;
import wt.vc.wip.WorkInProgressHelper;
import wt.vc.wip.Workable;

public class LoadFamilyTable {
	
	public static final boolean VERBOSE = true;
	public static String oldName = "";
	public static String oldVer  = "";
	public static String oldIter = "";

	public static void main ( String [] args ) throws Exception {
		if (args.length != 3) {
			System.out.println("\nUsage: java.ext.vpde.cadvaulting.CreateEPMDocs adminid adminpw loadfile");
		} else {
			if (LoadUtil.loginRemote(args[0],args[1])) {
				System.out.println("BEGIN CREATE FAMILY TABLES: " + Calendar.getInstance().getTime());
				readEntries(args[2]);
				System.out.println("-END- CREATE FAMILY TABLES: " + Calendar.getInstance().getTime());
			}
		}
    }
	
	private static void readEntries(String  LOADFILE) throws Exception {
		
		WINDSQL.openConnection();
		BufferedReader file = new BufferedReader(new FileReader(LOADFILE));
		String s = file.readLine();
		int lineNumber   = 0;
		while (s!= null && s.length() > 0) {	
			StringTokenizer strTok = new StringTokenizer(s,"|");
			processEntries(strTok);
			s = file.readLine();
			LoadUtil.printProgress(++lineNumber);
		}
		WINDSQL.closeConnection();
	}
	
	public static boolean processEntries(StringTokenizer strTok) throws Exception {
		String famIda2a2 = "";
		String fName = strTok.nextToken();
		String fVer  = strTok.nextToken();
		String fIter = strTok.nextToken();
		String fFlag = strTok.nextToken();
		String cName = strTok.nextToken();
		String cVer  = strTok.nextToken();
		String cIter = strTok.nextToken();
			
		String fDisp = fName + ", " + fVer + "." + fIter;
		String cDisp = cName + ", " + cVer + "." + cIter;
			
	    String conIda2a2 = WINDSQL.getIdFromDocumentName(cName,cVer,cIter);
	    String genIda2a2 = WINDSQL.getIdFromDocumentName(fName,fVer,fIter);
	    	
	    if (conIda2a2 == null) {
	    	System.out.println("INSTANCE NOT FOUND: " + cDisp);
	    	return false;
	    }
	    	
	    if (genIda2a2 == null) {
	    	System.out.println("GENERIC NOT FOUND: " + fDisp);
	    	return false;
	    }
	    	
	    if (fFlag.equals("1")) {
	    	if (VERBOSE) System.out.println("SAME: " + fDisp);
    		famIda2a2 = WINDSQL.getIdFromFamilyTableName(fDisp);
    		WINDSQL.createContainedInLink(cName,conIda2a2,famIda2a2);
	    } else {
	    	if (!oldName.equals(fName)) {
    			// new table
    			if (VERBOSE) System.out.println("NEW:  " + fDisp);
    			famIda2a2 = LoadUtil.createFamilyTable(genIda2a2, fName);
    			if (famIda2a2 != null) {
    				WINDSQL.updateNote(famIda2a2,fDisp);
    				WINDSQL.createContainedInLink(cName,conIda2a2,famIda2a2);
    				//SQL.updateFamilyTableIteration(famIda2a2, fIter);
    			} else {
    				System.out.println("UNABLE TO CREATE NEW TABLE: " + fDisp + " WITH " + cDisp);
    				throw new Exception("LOAD FAILED");
    			}
    		}
	    	
	    	if (oldName.equals(fName) && oldVer.equals(fVer) && !oldIter.equals(fIter)) {
    			//iterate it
    			
    			if (VERBOSE) System.out.println("ITER: " + fDisp);
    			try {
    				EPMSepFamilyTable esft = (EPMSepFamilyTable)BasicWebjectDelegate.getObjectByUfid(WINDSQL.getObidFromFamilyTableName(fName));
    				Folder folder = WorkInProgressHelper.service.getCheckoutFolder();
    				Workable workable = (Workable)esft;
    				CheckoutLink checkoutlink = WorkInProgressHelper.service.checkout(workable, folder, "");
    				Workable workable1 = checkoutlink.getWorkingCopy();
    				Persistable p = (Persistable)WorkInProgressHelper.service.checkin(workable1,"");
    				famIda2a2 = LoadUtil.getIdFromPersistable(p);
    				if (famIda2a2 != null) {
    					WINDSQL.updateFamilyTableIteration(famIda2a2,fIter);
    					WINDSQL.updateNote(famIda2a2,fDisp);
    					WINDSQL.removeFamilyTableLinks(famIda2a2);
    					WINDSQL.createContainedInLink(cName,conIda2a2,famIda2a2);
    				} else {
    					System.out.println("UNABLE TO ITERATE: " + fDisp + " WITH " + cDisp);
    					throw new Exception("LOAD FAILED");
    				}
    			} catch (Exception e) { 
    				System.out.println("UNABLE TO ITERATE: " + fDisp);
    				System.out.println(e.getLocalizedMessage());
    				throw new Exception("LOAD FAILED");
    			}
    		}
	    	
	    	if (oldName.equals(fName) && !oldVer.equals(fVer)) {
    			// revise
    			if (VERBOSE) System.out.println("REV:  " + fDisp);
    			famIda2a2 = LoadUtil.reviseFamilyTable(fName,fVer);
		    	if (famIda2a2 != null) {
		    		WINDSQL.updateNote(famIda2a2,fDisp);
		    		WINDSQL.removeFamilyTableLinks(famIda2a2);
		    		WINDSQL.createContainedInLink(cName,conIda2a2,famIda2a2);
		    	} else {
		    		System.out.println("UNABLE TO REVISE: " + fDisp + " WITH " + cDisp);
		    		throw new Exception("LOAD FAILED");
		    	}
    		}
	    }
    	oldName = fName;
    	oldVer  = fVer;
    	oldIter = fIter;
    	return true;
	}
}